<?php
/** UTF-8
 * Concentré Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Concentre
 * @package    Concentre_Archive
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version	   $ID$ r$Rev: 16 $ $Author: alban.lopez $ $Date: 2009-03-28 12:42:00 +0000 (Sat, 28 Mar 2009) $ 
 * @author	   Sébastien Cramattte <scramatte@zensoluciones.com>
 * @author	   Alban Lopez <alban.lopez+archive@gmail.com>
 * @see		   Based on EasyArchive <http://www.phpclasses.org/browse/package/4239.html>
 */


/**
 * @see Concentre_Archive_Abstract
 */
require_once 'Concentre/Archive/Abstract.php';

/**
 * @see Concentre_Archive_Interface
 */
require_once 'Concentre/Archive/Interface.php';


/**
 * @category   Concentre
 * @package    Concentre_Archive
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Concentre_Archive_Gzip extends Concentre_Archive_Abstract
{
	
	protected $_data;

    /**
     * Constructor
     *
     * @throws Concentre_Archive_Exception
     * @return void
     */
	public function __construct() {	
	}

    /**
     * Make archive
     *
     * @param  string $src
     * @param  string $dest
     * @return binary|string
     */
	public function make($src, $dest=null)
	{
		if (!file_exists($src)) {
			require_once 'Concentre/Archive/Exception.php';
			throw new Concentre_Archive_Exception(sprintf("Source file doesn't exists : %s", $src));
		}

		$data = gzencode( file_get_contents ($src) , 6);

		if (is_null($dest)) {
			return $data;
		} elseif (file_put_contents($dest, $data)) {
	 		return $dest;
		}

	}

    /**
     * Extract archive
     *
     * @param  string $src
     * @param  string $dest
     * @return binary|string
     */	
	public function extract($src, $dest=null)
	{
	
		if (!file_exists($src)) {
			require_once 'Concentre/Archive/Exception.php';
			throw new Concentre_Archive_Exception(sprintf("Source file doesn't exists : %s", $src));
		}
	
		$gzip = gzopen( $src, "r" );
		$data = '';
		
		while (!gzeof($gzip)) {
					$data .= gzread($gzip, 1024*1024);
		}
		
		gzclose( $gzip );
		
		if ($this->isTar($data)) {
			require_once 'Concentre/Archive/Tar.php';

			$tar = new Concentre_Archive_Tar();
			return $tar->extract($data);
	
		} else {
			if (is_null($dest)) {
				return $data;
			} elseif (file_put_contents($dest, $data)) {
				return $dest;
			}
		}
		
		return false;
	}

    /**
     * Get infos
     * 
     * @param  string $src
     * @return object
     */
	public function infos($src)
	{
	
		$content = new stdClass();
		$data = $this->extract($src);

		$content->Data = $data;
		$content->UnCompSize = strlen($data);
		$content->Size = filesize($src);
		$content->Ratio = strlen($data) ? round(100 - filesize($src) / strlen($data)*100, 1) : false;

		return $content;

	}	
}
